import { print_stack } from '../common/common.js'
import { enum_all_methods, enum_all_fields, enum_obj_fields } from '../common/dev.js'


function printIntent(intent) {
  if (!intent) {
      console.log(' ================ !intent')
      return;
  }

  // 把Intent当一个URI一样传递
  // https://blog.csdn.net/plokmju88/article/details/103354843
  console.log(' ================ intent: ', intent.toUri(0), ' =================== ')
  console.log(' ================ intent: ', decodeURIComponent(intent.toUri(0)), ' =================== ')
  console.log('typeof', typeof int64(0xff), typeof (0xff), typeof 0xff)
  console.log(' ================ intent: ', decodeURIComponent(intent.toUri(256)), ' =================== ')
  console.log('getAction()', intent.getAction())
  var extras = intent.getExtras()
  console.log('extras', JSON.stringify(extras), extras.toString())
  console.log('extras.keySet()', extras.keySet().toString(), JSON.stringify(extras.keySet()))

  try {
    var result = extras.getParcelableExtra('com.facebook.LoginFragment:Result');
    console.log('com.facebook.LoginFragment:Result', result)
  } catch (error) {
    console.log('error', error)
  }
  // for(var k of extras.keySet()) {
  //     console.log('k', k, extras.get(k))
  // }
}

export function hook_LoginManager() {
  let LoginManager = Java.use("com.facebook.login.LoginManager");
  LoginManager["getInstance"].implementation = function () {
      console.log(`LoginManager.getInstance is called`);
      let result = this["getInstance"]();
      console.log(`LoginManager.getInstance result=${result}`);
      // print_stack();
      return result;
  };
  LoginManager["onActivityResult"].overload('int', 'android.content.Intent').implementation = function (i, intent) {
      console.log(`LoginManager.onActivityResult is called: i=${i}, intent=${JSON.stringify(intent)}`);
      let result = this["onActivityResult"](i, intent);
      console.log(`LoginManager.onActivityResult result=${result}`);
      print_stack();
      printIntent(intent);
      return result;
  };
  // public boolean onActivityResult(int resultCode, Intent data, FacebookCallback<LoginResult> callback) {
  LoginManager["onActivityResult"].overload('int', 'android.content.Intent', 'com.facebook.FacebookCallback').implementation = function (resultCode, intent, callback) {
      console.log(`LoginManager.onActivityResult is called: resultCode=${resultCode}, data=${JSON.stringify(intent)}, callback=${callback}`);
      printIntent(intent)
      let result = this["onActivityResult"](resultCode, intent, callback);
      console.log(`LoginManager.onActivityResult result=${result}`);
      print_stack();
      printIntent(intent);
      return result;
  };

  dealLoginClientResult();
}

function print_AuthToken(at) {
  console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> enter print_AuthToken')
  // global.at = at
  // console.log('typeof at', typeof at, typeof at.value)
  // console.log('at.toString()', at.toString())
  // console.log('JSON.stringify(at)', JSON.stringify(at))
  // console.log('Object.keys(at)', Object.keys(at))
  // console.log('at.permissions', at.permissions)
  // console.log('enum_obj_fields', enum_obj_fields(at))
  console.log('enum_obj_fields at.value', enum_obj_fields(at.value))
  console.log('enum_obj_fields at.value.source.value', enum_obj_fields(at.value.source.value))
  console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> leave print_AuthToken')
}

function dealLoginClientResult() {
  // public static final Parcelable.Creator<LoginClient> CREATOR = new Parcelable.Creator<LoginClient>() { // from class: com.facebook.login.LoginClient$Companion$CREATOR$1
  // getParcelableExtra
  // var Activity = Java.use("android.content.Intent");
  let Result = Java.use("com.facebook.login.LoginClient$Result");
  Result["$init"].overload('android.os.Parcel', 'kotlin.jvm.internal.DefaultConstructorMarker').implementation = function (parcel, defaultConstructorMarker) {
      console.log(`Result.$init is called: parcel=${parcel}, defaultConstructorMarker=${defaultConstructorMarker}`);
      var result = this["$init"](parcel, defaultConstructorMarker);
      console.log('result', result)
  };
  // public static final Parcelable.Creator<Result> CREATOR = new Parcelable.Creator<Result>() { // from class: com.facebook.login.LoginClient$Result$Companion$CREATOR$1
  let LoginClient$Result$Companion$CREATOR$1 = Java.use("com.facebook.login.LoginClient$Result$Companion$CREATOR$1");
  LoginClient$Result$Companion$CREATOR$1["createFromParcel"].overload('android.os.Parcel').implementation = function (s) {
      console.log(`LoginClient$Result$Companion$CREATOR$1.createFromParcel is called`);
      print_stack();
      let result = this["createFromParcel"](s);
      console.log(`================= LoginClient$Result$Companion$CREATOR$1.createFromParcel result=${JSON.stringify(result)}`);
      console.log(`================= LoginClient$Result$Companion$CREATOR$1.createFromParcel result.toString()=${result.toString()}`);
      console.log('======= result.code', JSON.stringify(result.code))
      console.log('======= result.', result.code.toString())
      console.log('======= result.token', JSON.stringify(result.token))
      console.log('======= result.', result.token.toString())
      try {
        print_AuthToken(result.token)
        // console.log('================= token.token', result.token.getToken())
        // console.log('================= token.applicationId', result.token.getApplicationId())
        // console.log('================= token.userId', result.token.getUserId())
        // console.log('================= token.graphDomain', result.token.getGraphDomain())
        // console.log('================= token.source', result.token.getSource())
      } catch (error) {
        console.log('error applicationId', error)
      }
      console.log('======= result.loggingExtras', JSON.stringify(result.loggingExtras))
      console.log('======= result.', result.loggingExtras.toString())
      console.log('======= result.authenticationToken', JSON.stringify(result.authenticationToken))
      console.log('======= result.', result.authenticationToken.toString())
      // console.log(JSON.stringify(result.extraData), result.extraData.toString())
      // console.log(JSON.stringify(result.loggingExtras), result.loggingExtras.toString())
      // console.log(JSON.stringify(result), result.toString())
      // console.log(JSON.stringify(result.token), result.token.toString(), result.token.token)
      // console.log(JSON.stringify(result.request), result.request.toString())
      return result;
  };
}